/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import controllers.CobroController;
import domain.Cobro;
import java.util.List;

/**
 *
 * @author Julian
 */
public class CobrosFrm extends javax.swing.JFrame {

    /**
     * Creates new form CobrosFrm
     */
    public CobrosFrm() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        choiceAño = new java.awt.Choice();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableganancias = new javax.swing.JTable();
        lblAño = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        lblTotalCobrado = new javax.swing.JLabel();
        btnBuscarCobros = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cobros");

        tableganancias.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"Enero", null},
                {"Febrero", null},
                {"Marzo", null},
                {"Abril", null},
                {"Mayo", null},
                {"Junio", null},
                {"Julio", null},
                {"Agosto", null},
                {"Septiembre", null},
                {"Octubre", null},
                {"Noviembre", null},
                {"Diciembre", null}
            },
            new String [] {
                "Mes", "Total cobrado"
            }
        ));
        jScrollPane1.setViewportView(tableganancias);

        lblAño.setText("Año: ");

        jLabel6.setFont(new java.awt.Font("Perpetua Titling MT", 0, 18)); // NOI18N
        jLabel6.setText("UTN Facultad Regional San Francisco");

        jLabel7.setFont(new java.awt.Font("Perpetua Titling MT", 0, 14)); // NOI18N
        jLabel7.setText("Comedor Universitario");

        jLabel1.setText("Total: ");

        lblTotalCobrado.setText("TotalCobrado");

        btnBuscarCobros.setText("Buscar");
        btnBuscarCobros.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarCobrosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(44, 44, 44)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblAño)
                                .addGap(18, 18, 18)
                                .addComponent(choiceAño, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(btnBuscarCobros))
                        .addGap(66, 66, 66)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 309, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(171, 171, 171)
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(lblTotalCobrado))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(108, 108, 108)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(107, 107, 107)
                                .addComponent(jLabel7)
                                .addGap(98, 98, 98))
                            .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING))))
                .addContainerGap(68, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblAño)
                            .addComponent(choiceAño, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(26, 26, 26)
                        .addComponent(btnBuscarCobros))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 43, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lblTotalCobrado))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnBuscarCobrosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarCobrosActionPerformed
        restablecerTabla();
        
        rellenarTablaGanancias();
        
    }//GEN-LAST:event_btnBuscarCobrosActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CobrosFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CobrosFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CobrosFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CobrosFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CobrosFrm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscarCobros;
    private java.awt.Choice choiceAño;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblAño;
    private javax.swing.JLabel lblTotalCobrado;
    private javax.swing.JTable tableganancias;
    // End of variables declaration//GEN-END:variables

    private void rellenarTablaGanancias()
    {
        CobroController controladorCobro=new CobroController();
        int año=Integer.parseInt(choiceAño.getSelectedItem());
        List<Cobro> listaCobros=controladorCobro.listaCobrosDelAño(año);
        
        double[] totalDeLosMeses=new double[12];
        
        //recorrer los cobros y contar los de cada mes
        
        double totalDelAño=0;
        
        for(int i=0;i<listaCobros.size();i++)
        {
            Cobro cobroActual=listaCobros.get(i);
            int mesDeCobro=cobroActual.getFecha().getMonth();
            
            switch(mesDeCobro)
            {
                case 1:
                    totalDeLosMeses[0]+=cobroActual.getMonto();
                    break;
                  
                case 2:
                    totalDeLosMeses[1]+=cobroActual.getMonto();
                    break;
                    
                case 3:
                    totalDeLosMeses[2]+=cobroActual.getMonto();
                    break;
                    
                case 4:
                    totalDeLosMeses[3]+=cobroActual.getMonto();
                    break;
                    
                case 5:
                    totalDeLosMeses[4]+=cobroActual.getMonto();
                    break;
                    
                case 6:
                    totalDeLosMeses[5]+=cobroActual.getMonto();
                    break;
                    
                case 7:
                    totalDeLosMeses[6]+=cobroActual.getMonto();
                    break;
                    
                case 8:
                    totalDeLosMeses[7]+=cobroActual.getMonto();
                    break;
                    
                case 9:
                    totalDeLosMeses[8]+=cobroActual.getMonto();
                    break;
                    
                case 10:
                    totalDeLosMeses[9]+=cobroActual.getMonto();
                    break;
                    
                case 11:
                    totalDeLosMeses[10]+=cobroActual.getMonto();
                    break;
                    
                case 12:
                    totalDeLosMeses[11]+=cobroActual.getMonto();
                    break;
            }
            
            totalDelAño+=cobroActual.getMonto();
        }
        
        for(int i=0;i<12;i++)
        {
            tableganancias.setValueAt(totalDeLosMeses[i], i, 1);
        }
        
        lblTotalCobrado.setText(String.valueOf(totalDelAño));
    }
    
    private void restablecerTabla()
    {
        for(int i=0;i<12;i++)
        {
            tableganancias.setValueAt(0, i, 1);
        }
    }
}
